package com.jinqing.wuchuang.controller;

import com.jinqing.wuchuang.entity.Login;
import com.jinqing.wuchuang.response.RespBean;
import com.jinqing.wuchuang.service.AuthUserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@RestController()
@RequestMapping("auth")
public class AuthUserController {
    @Autowired
    private AuthUserService authService;


    @GetMapping("verification")
    public RespBean verification() throws IOException {
        return RespBean.ok("success", authService.GenerateVerificationCode());
    }

    /**
     * 注册新用户
     *
     * @param login
     * @return
     */
    @PostMapping("register")
    @ResponseBody
    public RespBean register(Login login) {
        //service抛异常这里处理
        //做参数检验
        int length = 0;
        switch (login.getMode()) {
            case "stu":
                length = 13;
                break;
            case "tel":
                length = 11;
        }
        if (login.getUsername().length() != length) {
            return RespBean.error("位数不符");
        } else {
            try {
                authService.register(login);
            } catch (Exception e) {
                return RespBean.error("注册失败", "");
            }
            return RespBean.ok("success", login.getUid());
        }
    }


}
